/*
 * Author: Jpeng
 * Email: peng8350@gmail.com
 * Time:  2019-06-26 16:28
 */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'
    hide RefreshIndicator, RefreshIndicatorState;
import 'package:flutter/widgets.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/*
   there two example implements two level,
   the first is common,when twoRefreshing,header will follow the list to scrollDown,when closing,still follow
   list move up,
   the second example use Navigator and keep offset when twoLevel trigger,
   header can use ClassicalHeader to implments twoLevel,it provide outerBuilder(1.4.7)
   important point:
   1. open enableTwiceRefresh bool ,default is false
   2. _refreshController.twiceRefreshComplete() can closing the two level
*/



import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Width Example'),
        ),
        body: TwoLevelExample(),
      ),
    );
  }
}
class TwoLevelExample extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TwoLevelExampleState();
  }
}

class _TwoLevelExampleState extends State<TwoLevelExample> {
  RefreshController _refreshController1 = RefreshController(initialRefreshStatus: RefreshStatus.twoLevelOpening);
  // RefreshController _refreshController1 = RefreshController();

  ScrollController sc = ScrollController();
  @override
  void initState() {
    sc.addListener(() {
      print("sc===============${sc.position}");
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return LayoutBuilder(
      builder: (_, c) {
        return SmartRefresher(
          child: Column(
            children: List.generate(10, (index) => Container(
              color: Colors.red,
              width: 100,
              height: 100,
              margin: EdgeInsets.all(10),
            )),
          ),
          controller: _refreshController1,
          scrollController: sc,
          enableTwoLevel: true,
          enablePullDown: true,
          enablePullUp: true,
          onLoading: () async {
            await Future.delayed(Duration(milliseconds: 2000));
            _refreshController1.loadComplete();
          },
          onRefresh: () async {
            await Future.delayed(Duration(milliseconds: 2000));
            _refreshController1.refreshCompleted();
          },
        );
      },
    );
  }
}

class TwoLevelWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      decoration: BoxDecoration(
        color: Colors.green,
        // image: DecorationImage(
        //     image: AssetImage("images/secondfloor.jpg"),
        //     // 很重要的属性,这会影响你打开二楼和关闭二楼的动画效果,关联到TwoLevelHeader,如果背景一致的情况,请设置相同
        //     alignment: Alignment.topCenter,
        //     fit: BoxFit.cover),
      ),
      child: Stack(
        children: <Widget>[
          Center(
            child: Wrap(
              children: <Widget>[
                TextButton(
                  onPressed: () {},
                  child: Text("登陆"),
                ),
              ],
            ),
          ),
          Container(
            height: 60.0,
            child: GestureDetector(
              child: Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
              ),
              onTap: () {
                SmartRefresher.of(context)?.controller.twoLevelComplete();
              },
            ),
            alignment: Alignment.bottomLeft,
          ),
        ],
      ),
    );
  }
}